package com.example.content.fragment.me

import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.donkingliang.imageselector.utils.ImageSelector
import com.example.content.DEFAULT_ITEM_TYPE
import com.example.content.R
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.databinding.FeedbackLayoutBinding
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.viewmodule.me.MyPageCommonViewModel
import com.example.core.extensions.showToast
import java.util.ArrayList

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/29
 */
class FeedBackFragment : BaseMvvmFragment<FeedbackLayoutBinding, MyPageCommonViewModel>() {

    private val footItemType = 52
    private lateinit var photosAdapter: RecyclerAdapter<String>
    private val footItem = ItemEntity(footItemType, "")
    //保存已选中的图片
    private val imageSelected = mutableListOf<String>()

    override fun initView() {
        photosAdapter = RecyclerAdapter(object : BindListener<String> {
            override fun convert(holder: BaseViewHolder, item: ItemEntity<String>) {
                when (holder.itemViewType) {
                    DEFAULT_ITEM_TYPE -> {
                        val image = holder.getView<ImageView>(R.id.image)
                        context?.let { Glide.with(it).load(item.data).into(image) }
                        val delete = holder.getView<View>(R.id.delete)
                        delete.setOnClickListener {
                            if (imageSelected.size > holder.layoutPosition) {
                                photosAdapter.data.removeAt(holder.layoutPosition)
                                imageSelected.removeAt(holder.layoutPosition)
                                if (imageSelected.size == 2) {
                                    photosAdapter.addData(footItem)
                                }
                                photosAdapter.notifyDataSetChanged()
                            }
                        }
                    }
                    footItemType -> {
                        val image = holder.getView<ImageView>(R.id.image)
                        image.setImageResource(R.drawable.ic_camera)
                        val deleteImage = holder.getView<View>(R.id.delete)
                        deleteImage.visibility = View.GONE
                    }
                }
            }
        })
        photosAdapter.addItemView(R.layout.item_upload_image_layout)
        photosAdapter.addItemViewByType(footItemType, R.layout.item_upload_image_layout)
        photosAdapter.addData(footItem)
        val layoutManager = GridLayoutManager(context, 3)
        dataBinding.recyclerView.layoutManager = layoutManager
        dataBinding.recyclerView.adapter = photosAdapter
    }

    override fun initListener() {
        dataBinding.topBar.backIcon.setOnClickListener {
            activity?.finish()
        }
        dataBinding.topBar.title.text = getString(R.string.feedback)
        dataBinding.submit.setOnClickListener {
            viewModel.feedback("反馈意见", emptyList())
        }

        photosAdapter.setOnItemClickListener { adapter, view, position ->
            if (adapter.getItemViewType(position) == footItemType) {
                ImageSelector.builder()
                    .useCamera(true)
                    .setMaxSelectCount(3)
                    .canPreview(true)
                    .setSelected(imageSelected as ArrayList<String>?)
                    .start(this, REQUEST_CODE)
            } else {
                // TODO: 2020/12/8 点击查看图片大图
            }
        }
        viewModel.responseResult.observe(this,
            {
                if (it) {
                    context?.showToast("反馈成功")
                    activity?.finish()
                } else {
                    context?.showToast("反馈失败")
                }
            })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == REQUEST_CODE && data != null) {
                val images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT) ?: return
                photosAdapter.data.clear()
                images.forEach {
                    photosAdapter.addData(ItemEntity(data = it))
                }
                imageSelected.clear()
                imageSelected.addAll(images)
                if (images.size < 3) {
                    photosAdapter.addData(footItem)
                }
            }
    }

    override fun getLayoutId() = R.layout.feedback_layout

    companion object {
        const val REQUEST_CODE = 2001
        fun newInstance() = FeedBackFragment()
    }

    override fun getViewModuleClass(): Class<MyPageCommonViewModel> {
        return MyPageCommonViewModel::class.java
    }
}